import 'package:flutter/material.dart';
import 'package:flutter_massage/common_lib/index.dart';
import 'package:flutter_massage/common_lib/routers/route.dart';
import 'package:flutter_massage/generated/assets.dart';
import 'package:flutter_massage/pages/message/message_bean.dart';
import 'package:flutter_massage/pages/message/message_detail_page.dart';
import 'package:flutter_massage/pages/web/web_view.dart';
import 'package:flutter_massage/service/index.dart';
import 'package:flutter_massage/values/index.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class MessageListItem extends StatelessWidget {
  const MessageListItem(this.chatBean, {Key? key}) : super(key: key);

  final ChatBean chatBean;

  @override
  Widget build(BuildContext context) {
    return ButtonWidget(
      onTap: () {
        if (chatBean.chatId.empty) return;
        CommonRoute.open(MessageDetailPage(chatId: chatBean.chatId!, name: chatBean.name ?? ''));
      },
      hasInkWell: false,
      child: Container(
        color: AppColors.white,
        padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 16.w),
        child: Row(
          children: [
            Stack(
              alignment: Alignment.bottomRight,
              children: [
                _buildHead(),
                // _buildNum(),
              ],
            ),
            SizedBox(width: 8.w),
            Expanded(
              child: SizedBox(
                height: 44.w,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    _buildName(),
                    SizedBox(height: 2.w),
                    _buildMessage(),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 头像
  Widget _buildHead() {
    return ImageWidget.url(
      chatBean.avatar ?? '',
      width: 44.w,
      height: 44.w,
      radius: 44.w,
      placeholder: Assets.svgMyIconPlaceholder.toSvg(width: 44.w),
    );
  }

  /// 昵称
  Widget _buildName() {
    return TextWidget.oneLine(
      text: chatBean.name ?? '',
      fontSize: 16,
      fontWeight: FontWeight.bold,
      color: AppColors.textLevelOne,
    );
  }

  ///内容
  Widget _buildMessage() {
    return TextWidget.oneLine(
      text: chatBean.message ?? '',
      fontSize: 14,
      color: AppColors.textLevelThree,
    );
  }

  // ///消息数
  // Widget _buildNum() {
  //   return Container(
  //     width: 15.w,
  //     height: 15.w,
  //     alignment: Alignment.center,
  //     decoration: CommonDecoration.getShapeDecoration(
  //       bg: AppColors.primaryOrange,
  //       radius: 15.w,
  //       borderWidth: 0.5.w,
  //       borderColor: AppColors.white,
  //     ),
  //     child: TextWidget.oneLine(
  //       text: '5',
  //       fontSize: 11,
  //       color: AppColors.white,
  //     ),
  //   );
  // }
}

class MessageListSystemItem extends StatelessWidget {
  const MessageListSystemItem({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ButtonWidget(
      onTap: () async {
        SettingBean bean = await XydUserService.getCacheMineInfoBean();
        if (bean.chatUrl.empty) return;
        CommonRoute.open(WebH5Page(url: bean.chatUrl!));
      },
      hasInkWell: false,
      child: Container(
        color: AppColors.white,
        padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 16.w),
        child: Row(
          children: [
            Stack(
              alignment: Alignment.bottomRight,
              children: [
                _buildHead(),
                // _buildNum(),
              ],
            ),
            SizedBox(width: 8.w),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  _buildName(),
                  SizedBox(height: 2.w),
                  _buildMessage(),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 头像
  Widget _buildHead() {
    return Assets.svgMessageCustomerAvatar.toSvg(width: 44.w);
  }

  /// 昵称
  Widget _buildName() {
    return const TextWidget.oneLine(
      text: '在线客服',
      fontSize: 16,
      fontWeight: FontWeight.bold,
      color: AppColors.black,
    );
  }

  ///内容
  Widget _buildMessage() {
    return TextWidget.oneLine(
      text: '您好，您您咨询的问题已为您回答。' * 2,
      fontSize: 14,
      color: AppColors.textLevelThree,
    );
  }

  // ///消息数
  // Widget _buildNum() {
  //   return Container(
  //     width: 15.w,
  //     height: 15.w,
  //     alignment: Alignment.center,
  //     decoration: CommonDecoration.getShapeDecoration(
  //       bg: AppColors.primaryOrange,
  //       radius: 15.w,
  //       borderWidth: 0.5.w,
  //       borderColor: AppColors.white,
  //     ),
  //     child: TextWidget.oneLine(
  //       text: '5',
  //       fontSize: 11,
  //       color: AppColors.white,
  //     ),
  //   );
  // }
}

class ImInput extends StatelessWidget {
  const ImInput({
    required this.controller,
    required this.focusNode,
    required this.onEditingComplete,
    required this.scrollController,
    this.hintText = "",
    this.fillColor,
    this.hintStyle,
    this.textStyle,
    this.inputBorder,
    this.contentPadding,
    this.maxLines = 5,
    this.minLines = 1,
    this.onChanged,
    Key? key,
  }) : super(key: key);

  final TextEditingController controller;
  final FocusNode? focusNode;
  final VoidCallback onEditingComplete;
  final String hintText;
  final Color? fillColor;
  final TextStyle? hintStyle;
  final TextStyle? textStyle;
  final InputBorder? inputBorder;
  final EdgeInsets? contentPadding;
  final int maxLines;
  final int minLines;
  final ScrollController scrollController;
  final ValueChanged<String>? onChanged;

  @override
  Widget build(BuildContext context) {
    return TextField(
      scrollController: scrollController,
      decoration: InputDecoration(
        hintText: hintText,
        hintStyle: hintStyle ?? const TextStyle(fontSize: 17, color: Colors.grey),
        fillColor: fillColor ?? Colors.white,
        filled: true,
        border: inputBorder ??
            OutlineInputBorder(
              borderRadius: BorderRadius.circular(6.w),
              borderSide: BorderSide.none,
            ),
        contentPadding: contentPadding ?? EdgeInsets.symmetric(horizontal: 16.w, vertical: 9.w),
        isCollapsed: true,
      ),
      style: textStyle ?? const TextStyle(fontSize: 17, color: Colors.black),
      controller: controller,
      focusNode: focusNode,
      keyboardType: TextInputType.text,
      maxLines: maxLines,
      minLines: minLines,
      textInputAction: TextInputAction.send,
      onEditingComplete: onEditingComplete,
      onChanged: onChanged,
    );
  }
}
